﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;

namespace RaisingForce.Raise.ObjectModel
{
    sealed public class StringProxy :
        IEquatable<string>,
        IEquatable<StringProxy>
    {
        #region Constructors

        public StringProxy(Func<string> getValue)
        {
            if (getValue == null)
            {
                throw new ArgumentNullException("getValue");
            }
            this.getValue = getValue;
        }

        #endregion

        #region Private Declarations

        private readonly Func<string> getValue;

        [ContractInvariantMethod]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.getValue != null);
        }

        #endregion

        #region Operator Overloads

        public static bool operator ==(StringProxy first, StringProxy second)
        {
            return Equals(first, second);
        }

        public static bool operator !=(StringProxy first, StringProxy second)
        {
            return !Equals(first, second);
        }

        public static bool operator ==(StringProxy first, string second)
        {
            return Equals(first, second);
        }

        public static bool operator !=(StringProxy first, string second)
        {
            return !Equals(first, second);
        }

        public static bool operator ==(string first, StringProxy second)
        {
            return Equals(second, first);
        }

        public static bool operator !=(string first, StringProxy second)
        {
            return !Equals(second, first);
        }

        public static implicit operator string(StringProxy value)
        {
            return object.ReferenceEquals(value, null) ?
                null :
                value.getValue();
        }

        #endregion

        #region Base Overrides

        public override bool Equals(object obj)
        {
            if (object.ReferenceEquals(obj, null))
            {
                return false;
            }
            if (object.ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj is StringProxy)
            {
                return this.Equals((StringProxy)obj);
            }
            if (obj is string)
            {
                return this.Equals((string)obj);
            }
            return false;
        }

        public override int GetHashCode()
        {
            string value = this.getValue();
            return value != null ? value.GetHashCode() : 0;
        }

        public override string ToString()
        {
            return this.getValue();
        }

        #endregion

        #region IEquatable<string> Members

        public bool Equals(string other)
        {
            if (other == null)
            {
                return false;
            }
            return other == this.getValue();
        }

        #endregion

        #region IEquatable<StringProxy> Members

        public bool Equals(StringProxy other)
        {
            if (object.ReferenceEquals(other, null))
            {
                return false;
            }
            return other.getValue() == this.getValue();
        }

        #endregion

        #region Static Members

        public static bool Equals(StringProxy first, StringProxy second)
        {
            if (object.ReferenceEquals(first, second))
            {
                return true;
            }
            if (object.ReferenceEquals(first, null)
                || object.ReferenceEquals(second, null))
            {
                return false;
            }
            return first.getValue() == second.getValue();
        }

        public static bool Equals(StringProxy first, string second)
        {
            if (object.ReferenceEquals(first, null)
                || object.ReferenceEquals(second, null))
            {
                return false;
            }
            return first.getValue() == second;
        }

        #endregion
    }
}
